package com.zhangmeng.tools.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.LineHandler;
import cn.hutool.core.io.file.Tailer;
import cn.hutool.core.lang.Console;
import com.sun.javafx.PlatformUtil;
import com.zhangmeng.tools.utils.AlertUtils;
import javafx.application.Platform;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

@Slf4j
public class LogConsoleController {

    @FXML
    public  TextArea console_show;

    @FXML
    private TextField log_path;

    @FXML
    private Button start_read;

    private Tailer tailer;

    private static final ObservableList<String> list =  FXCollections.observableArrayList();

    @FXML
    public void initialize() {

        console_show.setFont(Font.font("微软雅黑",14));
        new Thread(()->{
            list.addListener(new ListChangeListener<String>() {
                @Override
                public void onChanged(Change<? extends String> c) {
                    while (c.next()){
                        if (c.wasAdded()){
                            List<? extends String> addedSubList = c.getAddedSubList();
                            if (addedSubList.size()>0){
                                String line = addedSubList.get(0);
                                Platform.runLater(()->{
                                    console_show.appendText(System.lineSeparator());
                                    console_show.appendText(line);
                                    console_show.setScrollLeft(0);
                                });
                            }
                        }
                    }
                }
            });
        }).start();


        log_path.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (newValue != null && !newValue.equals("")){
                    log.info("log_path ==" + log_path.getText());
                    //重新加载日志
                    if (tailer != null){
                        tailer.stop();
                        start_read();
                    }
                }
            }
        });
        start_read.setOnAction(event -> {
            start_read();
        });
    }

    public  void start_read(){
        if (log_path.getText().length() == 0){
            AlertUtils.alert_warning("日志路径不能为空!");
            return;
        }
        File file = FileUtil.file(log_path.getText());

        String file_content = null;
        try {
            file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            try {
                file_content = Files.readString(file.toPath(), Charset.forName("GBK"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        console_show.setText(file_content);
        console_show.appendText(System.lineSeparator());
        console_show.setScrollLeft(0);
        new Thread(()->{
            LineHandler CONSOLE_HANDLER = new ConsoleLineHandler();
            tailer = new Tailer(file, CONSOLE_HANDLER, 2);
            tailer.start();
        }).start();
    }

    /**
     * 命令行打印的行处理器
     *
     * @author looly
     * @since 4.5.2
     */
    public static class ConsoleLineHandler implements LineHandler {
        @Override
        public void handle(String line) {
            list.add(line);
        }
    }
}
